﻿using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using Lamex.Base;
using Lamex.Interface;
using Lamex.Library;
using Lamex.mMachine.BLL;
using Lamex.mMachine.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mMachine.UI
{
    public partial class frmMAC_Equipment_Basic_Info_New : frmBaseChild
    {
        string IsEdit = "";
        public frmMAC_Equipment_Basic_Info_New()
        {
            InitializeComponent();
        }
        //设置控件可选项
        private void setvGridControlRepository()
        {
            vGridControl1.OptionsBehavior.Editable = false;
            vGridControl1.DataSource = null;

            //获取申购编号
            string sql = @"  SELECT MACHINE_APPLIED_NO,APPLIED_USER FROM [LamexMESManager].[dbo].[FAMA_Account_Purchase_plan]";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);

            DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit lookup_applied = new DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit();
            lookup_applied.NullText = string.Empty;
            if (dt.Rows.Count > 0)
            {
                lookup_applied.DataSource = dt;
                lookup_applied.ValueMember = "MACHINE_APPLIED_NO";
                lookup_applied.DisplayMember = "MACHINE_APPLIED_NO";
            }
            vGridControl1.Rows["rowMACHINE_APPLIED_NO"].Properties.RowEdit = lookup_applied;

            //获取combox下拉值
            Common.GetDataTable comm = new Common.GetDataTable();
            DataTable dtComlist = comm.GetValuesOption();
            //设备等级
            DevExpress.XtraEditors.Repository.RepositoryItemComboBox comboBox_level = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
            comboBox_level.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            DataTable dt_level = dtComlist.Select("VALUE_NAME='设备等级'").CopyToDataTable();
            string[] values = dt_level.Rows[0]["VALUE_OPTION"].ToString().Split(';');
            foreach (var item in values)
            {
                comboBox_level.Items.Add(item);
            }

            vGridControl1.Rows["rowMACHINE_LEVEL"].Properties.RowEdit = comboBox_level;

            //设备产地
            DevExpress.XtraEditors.Repository.RepositoryItemComboBox comboBox_orgin = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
            comboBox_orgin.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            DataTable dt_orgin = dtComlist.Select("VALUE_NAME='设备产地'").CopyToDataTable();
            string[] values_orgin = dt_orgin.Rows[0]["VALUE_OPTION"].ToString().Split(';');
            foreach (var item in values_orgin)
            {
                comboBox_orgin.Items.Add(item);
                txtMACHINE_ORIGIN.Properties.Items.Add(item);
            }
            vGridControl1.Rows["rowMACHINE_ORIGIN"].Properties.RowEdit = comboBox_orgin;

            //设备类型
            DevExpress.XtraEditors.Repository.RepositoryItemComboBox comboBox_type = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
            comboBox_type.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            DataTable dt_type = dtComlist.Select("VALUE_NAME='设备类别'").CopyToDataTable();
            string[] values_type = dt_type.Rows[0]["VALUE_OPTION"].ToString().Split(';');
            foreach (var item in values_type)
            {
                comboBox_type.Items.Add(item);
                txtMACHINE_TYPE.Properties.Items.Add(item);
            }
            vGridControl1.Rows["rowMACHINE_TYPE"].Properties.RowEdit = comboBox_type;

            DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit memoEdit = new DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit();
            vGridControl1.Rows["rowMACHINE_DESC"].Properties.RowEdit = memoEdit;
            ;

        }
        private void frmMAC_Equipment_Basic_Info_New_Resize(object sender, EventArgs e)
        {
            splitContainerControl1.SplitterPosition = this.Width - 330;
        }

        private void frmMAC_Equipment_Basic_Info_New_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            setvGridControlRepository();
            GetSearchLookUpEdit_MachineNo();
            gridControl1.UseEmbeddedNavigator = true;
        }
        public override void DoAdd(Lamex.Interface.IButtonInfo sender)
        {
            //选项为故障上报时才可以新增

            vGridControl1.OptionsBehavior.Editable = true;
            if (vGridControl1.Rows.Count > 0)
                for (int i = 0; i < vGridControl1.Rows.Count; i++)
                {
                    vGridControl1.SetCellValue(vGridControl1.Rows[i], 0, null);
                }
            IsEdit = "Add";

        }

        public override void DoView(Lamex.Interface.IButtonInfo sender)
        {
            string sqlWhere = "";
            string sql = @"SELECT [MACHINE_NO]
      ,[MACHINE_APPLIED_NO]
      ,[MACHINE_SN]
      ,[MACHINE_NAME]
      ,[MACHINE_DESC]
      ,[MACHINE_TYPE]
      ,[MACHINE_LEVEL]
      ,[MACHINE_BRAND]
      ,[MACHINE_POWER]
      ,[MACHINE_ORIGIN]
      ,CONVERT(VARCHAR(10),ONSITE_DATE,120) ONSITE_DATE
      ,CONVERT(VARCHAR(10),DEBUGGING_DATE,120) DEBUGGING_DATE
      ,CONVERT(VARCHAR(10),TRIAL_RUN_DATE,120) TRIAL_RUN_DATE
      ,CONVERT(VARCHAR(10),TRAINING_DATE,120) TRAINING_DATE
      ,CONVERT(VARCHAR(10),ACCEPTANCE_DATE,120) ACCEPTANCE_DATE
      ,CONVERT(VARCHAR(10),COMMISSIONING_DATE,120) COMMISSIONING_DATE
      ,[UPDATE_TIME]
      ,case when isnull(m.UserName,'')!='' then m.UserName else i.UPDATE_USER end as [UPDATE_USER]
  FROM [LamexMESManager].[dbo].[FAMA_Account_Basic_Info] i left join
  [LamexMESManager].[dbo].[Sys_MyUser] m on i.UPDATE_USER=m.Account WHERE 1=1";
            if (!string.IsNullOrEmpty(txtMACHINE_NO.Text))
            {
                sqlWhere += " and MACHINE_NO like '%" + txtMACHINE_NO.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtMACHINE_ORIGIN.Text))
            {
                sqlWhere += " and MACHINE_ORIGIN like '%" + txtMACHINE_ORIGIN.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtMACHINE_TYPE.Text))
            {
                sqlWhere += " and MACHINE_TYPE like '%" + txtMACHINE_TYPE.Text + "%'";
            }

            sql = sql + sqlWhere + " order by UPDATE_TIME desc ";
            gridControl1.DataSource = null;
            DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql);
            gridControl1.DataSource = dt;
            gridView1.BestFitColumns();
            this.gridView1.OptionsView.ColumnAutoWidth = false;

        }
        public override void DoEdit(Lamex.Interface.IButtonInfo sender)
        {
            SetVGridControl();
        }
        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            SetVGridControl();
        }
        private void SetVGridControl()
        {
            //不提供单击浏览操作，浏览、编辑共用操作
            vGridControl1.OptionsBehavior.Editable = true;
            IsEdit = "Edit";
            //获取当前行的数据
            int iRow = gridView1.FocusedRowHandle;
            string strMACHINE_NO = gridView1.GetRowCellDisplayText(iRow, "MACHINE_NO").ToString();//设备编号
            string strMACHINE_APPLIED_NO = gridView1.GetRowCellDisplayText(iRow, "MACHINE_APPLIED_NO").ToString();//申购编号
            string strMACHINE_SN = gridView1.GetRowCellDisplayText(iRow, "MACHINE_SN").ToString();//设备型号
            string strMACHINE_NAME = gridView1.GetRowCellDisplayText(iRow, "MACHINE_NAME").ToString();//设备名称
            string strMACHINE_DESC = gridView1.GetRowCellDisplayText(iRow, "MACHINE_DESC").ToString();//设备描述
            string strMACHINE_TYPE = gridView1.GetRowCellDisplayText(iRow, "MACHINE_TYPE").ToString();//设备类型
            string strMACHINE_LEVEL = gridView1.GetRowCellDisplayText(iRow, "MACHINE_LEVEL").ToString();//设备等级
            string strMACHINE_BRAND = gridView1.GetRowCellDisplayText(iRow, "MACHINE_BRAND").ToString();//设备品牌
            string strMACHINE_POWER = gridView1.GetRowCellDisplayText(iRow, "MACHINE_POWER").ToString();//设备功率
            string strMACHINE_ORIGIN = gridView1.GetRowCellDisplayText(iRow, "MACHINE_ORIGIN").ToString();//设备产地

            string strONSITE_DATE = gridView1.GetRowCellDisplayText(iRow, "ONSITE_DATE").ToString();//设备到厂时间
            string strDEBUGGING_DATE = gridView1.GetRowCellDisplayText(iRow, "DEBUGGING_DATE").ToString();//调试完成时间
            string strTRIAL_RUN_DATE = gridView1.GetRowCellDisplayText(iRow, "TRIAL_RUN_DATE").ToString();//试产完成时间
            string strTRAINING_DATE = gridView1.GetRowCellDisplayText(iRow, "TRAINING_DATE").ToString();//培训完成时间
            string strACCEPTANCE_DATE = gridView1.GetRowCellDisplayText(iRow, "ACCEPTANCE_DATE").ToString();//设备验收时间
            string strCOMMISSIONING_DATE = gridView1.GetRowCellDisplayText(iRow, "COMMISSIONING_DATE").ToString();//设备投产时间

            //把数据显示到右边的VGridControl
            vGridControl1.Rows["rowMACHINE_NO"].Properties.Value = strMACHINE_NO;
            vGridControl1.Rows["rowMACHINE_APPLIED_NO"].Properties.Value = strMACHINE_APPLIED_NO;
            vGridControl1.Rows["rowMACHINE_SN"].Properties.Value = strMACHINE_SN;
            vGridControl1.Rows["rowMACHINE_NAME"].Properties.Value = strMACHINE_NAME;
            vGridControl1.Rows["rowMACHINE_DESC"].Properties.Value = strMACHINE_DESC;
            vGridControl1.Rows["rowMACHINE_TYPE"].Properties.Value = strMACHINE_TYPE;
            vGridControl1.Rows["rowMACHINE_LEVEL"].Properties.Value = strMACHINE_LEVEL;
            vGridControl1.Rows["rowMACHINE_BRAND"].Properties.Value = strMACHINE_BRAND;
            vGridControl1.Rows["rowMACHINE_POWER"].Properties.Value = strMACHINE_POWER;
            vGridControl1.Rows["rowMACHINE_ORIGIN"].Properties.Value = strMACHINE_ORIGIN;

            vGridControl1.Rows["rowONSITE_DATE"].Properties.Value = strONSITE_DATE;
            vGridControl1.Rows["rowDEBUGGING_DATE"].Properties.Value = strDEBUGGING_DATE;
            vGridControl1.Rows["rowTRIAL_RUN_DATE"].Properties.Value = strTRIAL_RUN_DATE;
            vGridControl1.Rows["rowTRAINING_DATE"].Properties.Value = strTRAINING_DATE;
            vGridControl1.Rows["rowACCEPTANCE_DATE"].Properties.Value = strACCEPTANCE_DATE;
            vGridControl1.Rows["rowCOMMISSIONING_DATE"].Properties.Value = strCOMMISSIONING_DATE;
        }
        public void GetSearchLookUpEdit_MachineNo()
        {
            txtMACHINE_NO.Text = string.Empty;
            string sql = @" SELECT MACHINE_NO,MACHINE_NAME,MACHINE_DESC FROM [LamexMESManager].[dbo].[FAMA_Account_Basic_Info]";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            txtMACHINE_NO.Properties.ValueMember = "MACHINE_NO";
            txtMACHINE_NO.Properties.DisplayMember = "MACHINE_NO";
            txtMACHINE_NO.Properties.DataSource = dt;
        }

        private void sbtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string strMACHINE_APPLIED_NO = vGridControl1.Rows["rowMACHINE_APPLIED_NO"].Properties.Value != null ?
                  vGridControl1.Rows["rowMACHINE_APPLIED_NO"].Properties.Value.ToString() : "";//申报编号
                string strMACHINE_SN = vGridControl1.Rows["rowMACHINE_SN"].Properties.Value != null ?
                    vGridControl1.Rows["rowMACHINE_SN"].Properties.Value.ToString() : "";//设备型号
                string strMACHINE_NAME = vGridControl1.Rows["rowMACHINE_NAME"].Properties.Value != null ?
                    vGridControl1.Rows["rowMACHINE_NAME"].Properties.Value.ToString() : "";//设备名称
                string strMACHINE_DESC = vGridControl1.Rows["rowMACHINE_DESC"].Properties.Value != null ?
                    vGridControl1.Rows["rowMACHINE_DESC"].Properties.Value.ToString() : "";//设备描述
                string strMACHINE_TYPE = vGridControl1.Rows["rowMACHINE_TYPE"].Properties.Value != null ?
                    vGridControl1.Rows["rowMACHINE_TYPE"].Properties.Value.ToString() : "";//设备类型
                string strMACHINE_LEVEL = vGridControl1.Rows["rowMACHINE_LEVEL"].Properties.Value != null ?
                    vGridControl1.Rows["rowMACHINE_LEVEL"].Properties.Value.ToString().ToUpper() : "";//设备等级
                string strMACHINE_BRAND = vGridControl1.Rows["rowMACHINE_BRAND"].Properties.Value != null ?
                      vGridControl1.Rows["rowMACHINE_BRAND"].Properties.Value.ToString() : "";//设备品牌
                string strMACHINE_POWER = vGridControl1.Rows["rowMACHINE_POWER"].Properties.Value != null ?
                    vGridControl1.Rows["rowMACHINE_POWER"].Properties.Value.ToString() : "";//功率
                string strMACHINE_ORIGIN = vGridControl1.Rows["rowMACHINE_ORIGIN"].Properties.Value != null ?
                    vGridControl1.Rows["rowMACHINE_ORIGIN"].Properties.Value.ToString().ToUpper() : "";//设备产地


                string strONSITE_DATE = vGridControl1.Rows["rowONSITE_DATE"].Properties.Value != null ?
                  vGridControl1.Rows["rowONSITE_DATE"].Properties.Value.ToString().ToUpper() : null;//设备到厂时间
                string strDEBUGGING_DATE = vGridControl1.Rows["rowDEBUGGING_DATE"].Properties.Value != null ?
                vGridControl1.Rows["rowDEBUGGING_DATE"].Properties.Value.ToString().ToUpper() : null;//调试完成时间
                string strTRIAL_RUN_DATE = vGridControl1.Rows["rowTRIAL_RUN_DATE"].Properties.Value != null ?
                vGridControl1.Rows["rowTRIAL_RUN_DATE"].Properties.Value.ToString().ToUpper() : null;//试产完成时间
                string strTRAINING_DATE = vGridControl1.Rows["rowTRAINING_DATE"].Properties.Value != null ?
                vGridControl1.Rows["rowTRAINING_DATE"].Properties.Value.ToString().ToUpper() : null;//培训完成时间
                string strACCEPTANCE_DATE = vGridControl1.Rows["rowACCEPTANCE_DATE"].Properties.Value != null ?
                vGridControl1.Rows["rowACCEPTANCE_DATE"].Properties.Value.ToString().ToUpper() : null;//设备验收时间
                string strCOMMISSIONING_DATE = vGridControl1.Rows["rowCOMMISSIONING_DATE"].Properties.Value != null ?
                vGridControl1.Rows["rowCOMMISSIONING_DATE"].Properties.Value.ToString().ToUpper() : null;//设备投产时间

                //string strUPDATE_TIME = DateTime.Now.ToString();
                //string strUPDATE_USERID = Lamex.Common.Loginer.CurrentUser.Account;


                if (string.IsNullOrEmpty(strMACHINE_SN))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("设备型号不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (string.IsNullOrEmpty(strMACHINE_NAME))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("设备名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (string.IsNullOrEmpty(strMACHINE_DESC))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("设备描述不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (string.IsNullOrEmpty(strMACHINE_BRAND))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("设备品牌不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (IsEdit == "Add")
                {
                    FAMA_Account_Basic_InfoInfo model = new FAMA_Account_Basic_InfoInfo();
                    model.MACHINE_NO = Common.SerialNoHelper.Generate();//设备编号
                    model.MACHINE_APPLIED_NO = strMACHINE_APPLIED_NO;//申购编号
                    model.MACHINE_SN = strMACHINE_SN;//设备型号
                    model.MACHINE_NAME = strMACHINE_NAME;//设备名称
                    model.MACHINE_DESC = strMACHINE_DESC;//设备描述
                    model.MACHINE_TYPE = strMACHINE_TYPE;//设备类别
                    model.MACHINE_LEVEL = strMACHINE_LEVEL;//设备等级
                    model.MACHINE_BRAND = strMACHINE_BRAND;//设备品牌
                    model.MACHINE_POWER = Convert.ToDecimal(strMACHINE_POWER);//设备功率
                    model.MACHINE_ORIGIN = strMACHINE_ORIGIN;//设备产地
                    model.ONSITE_DATE = strONSITE_DATE;//设备到厂时间
                    model.DEBUGGING_DATE = strDEBUGGING_DATE;//调试完成时间
                    model.TRIAL_RUN_DATE = strTRIAL_RUN_DATE;//试产完成时间
                    model.TRAINING_DATE = strTRAINING_DATE;//培训完成时间
                    model.ACCEPTANCE_DATE = strACCEPTANCE_DATE;//设备验收时间
                    model.COMMISSIONING_DATE = strCOMMISSIONING_DATE;//设备投产时间
                    model.UPDATE_TIME = DateTime.Now;//创建时间
                    model.UPDATE_USER = Lamex.Common.Loginer.CurrentUser.Account;//修改人

                    if (BaseFactory<FAMA_Account_Basic_Info>.Instance.InsertKey(model))
                        DevExpress.XtraEditors.XtraMessageBox.Show("新增成功", "提示", MessageBoxButtons.OK);
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("新增失败", "提示", MessageBoxButtons.OK);
                }
                if (IsEdit == "Edit")
                {
                    string strMACHINE_NO = vGridControl1.Rows["rowMACHINE_NO"].Properties.Value != null ?
                vGridControl1.Rows["rowMACHINE_NO"].Properties.Value.ToString() : "";//设备编号
                    //采用直接更新方式，避免日期为空时显示1900-01-01
                    string STRONSITE = strONSITE_DATE.Length > 0 ? "'" + strONSITE_DATE + "'" : "NULL";
                    string STRDEBUGGING = strDEBUGGING_DATE.Length > 0 ? "'" + strDEBUGGING_DATE + "'" : "NULL";
                    string STRTRIAL_RUN = strTRIAL_RUN_DATE.Length > 0 ? "'" + strTRIAL_RUN_DATE + "'" : "NULL";
                    string STRTRAINING = strTRAINING_DATE.Length > 0 ? "'" + strTRAINING_DATE + "'" : "NULL";
                    string STRACCEPTANCE = strACCEPTANCE_DATE.Length > 0 ? "'" + strACCEPTANCE_DATE + "'" : "NULL";
                    string STRCOMMISSIONING = strCOMMISSIONING_DATE.Length > 0 ? "'" + strCOMMISSIONING_DATE + "'" : "NULL";


                    string sql = @"UPDATE  [LamexMESManager].[dbo].[FAMA_Account_Basic_Info] SET
       [MACHINE_APPLIED_NO]='" + strMACHINE_APPLIED_NO + @"'
      ,[MACHINE_SN]='" + strMACHINE_SN + @"'
      ,[MACHINE_NAME]='" + strMACHINE_NAME + @"'
      ,[MACHINE_DESC]='" + strMACHINE_DESC + @"'
      ,[MACHINE_TYPE]='" + strMACHINE_TYPE + @"'
      ,[MACHINE_LEVEL]='" + strMACHINE_LEVEL + @"'
      ,[MACHINE_BRAND]='" + strMACHINE_BRAND + @"'
      ,[MACHINE_POWER]='" + strMACHINE_POWER + @"'
      ,[MACHINE_ORIGIN]='" + strMACHINE_ORIGIN + @"'
      ,[ONSITE_DATE]=" + STRONSITE + @"
      ,[DEBUGGING_DATE]=" + STRDEBUGGING + @"
      ,[TRIAL_RUN_DATE]=" + STRTRIAL_RUN + @"
      ,[TRAINING_DATE]=" + STRTRAINING + @"
      ,[ACCEPTANCE_DATE]=" + STRACCEPTANCE + @"
      ,[COMMISSIONING_DATE]=" + STRCOMMISSIONING + @"
      ,UPDATE_TIME=GETDATE()
      ,UPDATE_USER='" + Lamex.Common.Loginer.CurrentUser.Account + @"'
	  WHERE MACHINE_NO= '" + strMACHINE_NO + "'";
                    if (BaseBuildSql.ExecuteNonQuery(sql) > 0)
                        DevExpress.XtraEditors.XtraMessageBox.Show("修改成功", "提示", MessageBoxButtons.OK);
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("修改失败", "提示", MessageBoxButtons.OK);
                }
                //每次保存后锁定信息输入
                vGridControl1.OptionsBehavior.Editable = false;
                for (int i = 0; i < vGridControl1.Rows.Count; i++)
                {
                    vGridControl1.SetCellValue(vGridControl1.Rows[i], 0, null);
                }
                DoView(null);

            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public override void DoSave(Lamex.Interface.IButtonInfo sender)
        {
            sbtnSave_Click(null, null);
        }
        public override void DoExport(IButtonInfo sender)
        {
            if (!Directory.Exists(@"D:\MESTemp"))
                Directory.CreateDirectory(@"D:\MESTemp");
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.InitialDirectory = @"D:\MESTemp";//导出默认路径
            fileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";//导出默认格式
            fileDialog.FileName = "MES_TEMP_" + DateTime.Now.ToString("yyMMdd_hhMMss");//导出默认名称
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                //导出表【IE_Part_TaskList】中当前产品类型，无“工艺路线标识”的数据

                string sqlWhere = "";
                string sql = @"SELECT [MACHINE_NO]
      ,[MACHINE_APPLIED_NO]
      ,[MACHINE_SN]
      ,[MACHINE_NAME]
      ,[MACHINE_DESC]
      ,[MACHINE_TYPE]
      ,[MACHINE_LEVEL]
      ,[MACHINE_BRAND]
      ,[MACHINE_POWER]
      ,[MACHINE_ORIGIN]
      ,CONVERT(VARCHAR(10),ONSITE_DATE,120) ONSITE_DATE
      ,CONVERT(VARCHAR(10),DEBUGGING_DATE,120) DEBUGGING_DATE
      ,CONVERT(VARCHAR(10),TRIAL_RUN_DATE,120) TRIAL_RUN_DATE
      ,CONVERT(VARCHAR(10),TRAINING_DATE,120) TRAINING_DATE
      ,CONVERT(VARCHAR(10),ACCEPTANCE_DATE,120) ACCEPTANCE_DATE
      ,CONVERT(VARCHAR(10),COMMISSIONING_DATE,120) COMMISSIONING_DATE
      ,[UPDATE_TIME]
      ,[UPDATE_USER]
  FROM [LamexMESManager].[dbo].[FAMA_Account_Basic_Info] WHERE 1=1";
                if (!string.IsNullOrEmpty(txtMACHINE_NO.Text))
                {
                    sqlWhere += " and MACHINE_NO like '%" + txtMACHINE_NO.Text + "%'";
                }
                if (!string.IsNullOrEmpty(txtMACHINE_ORIGIN.Text))
                {
                    sqlWhere += " and MACHINE_ORIGIN like '%" + txtMACHINE_ORIGIN.Text + "%'";
                }
                if (!string.IsNullOrEmpty(txtMACHINE_TYPE.Text))
                {
                    sqlWhere += " and MACHINE_TYPE like '%" + txtMACHINE_TYPE.Text + "%'";
                }

                sql = sql + sqlWhere + " order by UPDATE_TIME desc ";
                DataTable dtList = BaseBuildSql.GetDataTableBySql(sql);

                GridControl grid = new GridControl();
                GridView view = new GridView();
                grid.ViewCollection.Add(view);
                grid.MainView = view;
                view.GridControl = grid;
                foreach (DataColumn dc in dtList.Columns)
                {
                    DevExpress.XtraGrid.Columns.GridColumn gridColumnNumber = view.Columns.AddVisible(dc.ColumnName);
                    gridColumnNumber.FieldName = dc.ColumnName;
                }
                grid.DataSource = dtList;
                this.Controls.Add(grid);
                grid.ForceInitialize();//强制初始化
                //Font font = new Font("宋体", 11, FontStyle.Bold); 
                //view.Appearance.HeaderPanel.Font = font;
                //view.Appearance.Row.Font = font;            
                view.BestFitColumns();
                view.OptionsView.ColumnAutoWidth = false;
                view.OptionsView.ShowAutoFilterRow = false;
                view.OptionsPrint.PrintHorzLines = false;//去掉水平网格线
                view.OptionsPrint.PrintVertLines = false;//去掉垂直网格线
                view.ExportToXlsx(fileDialog.FileName);
                if (DevExpress.XtraEditors.XtraMessageBox.Show("导出成功，是否打开文件？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    System.Diagnostics.Process.Start(fileDialog.FileName);//打开指定路径下的文件

            }
        }

        private void sbtnView_Click(object sender, EventArgs e)
        {
            DoView(null);
        }
    }
}
